
#!/bin/bash

:<<EOF

按时间还原点  把北部湾历史商户入库;
为了安全起见, 项目 是不存在任何敏感配置的 ;
但是在 脚本服务器 必须含有 /opt/trx-data-importer/config/mysql_client_prod.conf 这个 MySQL 配置文件;
千万不要忘记跑 销户日期脚本;


跑当天    bash  run_bbw_mer_history.sh  -s 2024-08-01 -e   2024-08-01



跑历史    bash  run_bbw_mer_history.sh  -s 2025-08-21 -e   2025-08-25
跑历史    bash  run_bbw_mer_history.sh  -s 2025-08-26 -e   2025-08-26

看更新截止日期  select MAX(folder_date) from ftp_vsp_merchant_history_bbw ;



EOF

readonly  BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $BASE_DIR || exit;

shopt -s expand_aliases
[ "$(uname)" == "Darwin" ] &&  alias date='gdate'


pwd
source  $BASE_DIR"/bbw_history_mer_sql.sh"

CONFIG_FILE=/opt/trx-data-importer/config/mysql_client_prod.conf
start=$(date -d "yesterday" +%Y-%m-%d)
end=$(date -d "yesterday" +%Y-%m-%d)
FORCE_EXPORT='NO'
while getopts "Dfs:e:O:" opt; do
    case ${opt} in
    s )
        start="$OPTARG"
        ;;
    e )
        end="$OPTARG"
        ;;
    O )
        OUTPUT_DIR="$OPTARG"
        ;;
    D )
        DRY_RUN="YES"
        ;;
    f )
        FORCE_EXPORT="YES"
        FORCE_EXPORT_ARG=""
        ;;
    *)
        echo "Invalid option: $opt requires an argument" 1>&2
        ;;
    esac
done
shift $((OPTIND -1))

echo "start ==> $start"
echo "end   ==> $end"
echo "FORCE_EXPORT   ==> $FORCE_EXPORT"


while ! [[ $start > $end ]]; do
    year_month=$(date -d  "${start}" +%Y%m)   #  yyyy-MM-dd  ==>  yyyymm
    day=$(date -d "$start" +%d)            #  yyyy-MM-dd   ==> dd
    echo "[$(date)] executing  ${year_month}/${day}"


    if  [ -e   /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/merrecord_V2_history_all.zip   ]  && [[ $FORCE_EXPORT != 'YES'  ]] ; then
            echo "已存在重新导入的月分商户文件, 且没有强制参数, 不再复制"
    else
            cp   /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/merrecord_V2.zip     /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/merrecord_V2_history_all.zip
    fi


    if  [[ -e /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/c_merrecord_V2_history_all.zip   ]] && [[ $FORCE_EXPORT != 'YES'  ]]   ; then
            echo "已存在重新导入的日分商户文件, 且没有强制参数, 不再复制"
    else
            echo "不存在重新导入的日分商户文件 或者 有强制参数, 重新复制"
            if [[ -e /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/c_merrecord_V2_origin.zip  ]]; then
                echo  "优先调取未脱敏的数据  c_merrecord_V2_origin.zip ...."
                cp   /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/c_merrecord_V2_origin.zip   /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/c_merrecord_V2_history_all.zip
            else
                echo  "调取 c_merrecord_V2.zip ........ 数据 "
                cp   /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/c_merrecord_V2.zip   /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/c_merrecord_V2_history_all.zip
            fi
    fi



    echo  "/opt/trx-data-importer/bin/console import -b gx  --environment=prod  -t  ${start} -f   /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/merrecord_V2_history_all.zip "

    echo  "/opt/trx-data-importer/bin/console import -b gx  --environment=prod  -t  ${start} -f   /opt/trx-data-importer/data/trx-data-files/gx/${year_month}/${day}/c_merrecord_V2_history_all.zip "


    if  [[ -e  /opt/trx-data-importer/data/trx-data-files/gx/$year_month/$day/merrecord_V2_history_all.zip ]]; then
        /opt/trx-data-importer/bin/console import -b gx  --environment=prod  -t  $start -f   /opt/trx-data-importer/data/trx-data-files/gx/$year_month/$day/merrecord_V2_history_all.zip
    else
        echo " $start  没有全量商户, 不导入全量操作"
    fi

    echo " 加载增量文件入库  "
    # /opt/trx-data-importer/bin/console import -b gx  --environment=prod  -t  $start -f   /opt/trx-data-importer/data/trx-data-files/gx/$year_month/$day/c_merrecord_V2_history_all.zip


    # execute_sql=$SQL
    # execute_sql=${execute_sql//'#param'/$start}
    # echo  "executing...........      $execute_sql"
    # mysql --defaults-file=$CONFIG_FILE -e "$execute_sql"

    # echo "executing .......   $AFTER_SQL1"
    # echo "executing .......   $AFTER_SQL2"
    # echo "executing .......   $AFTER_SQL3"
    # mysql --defaults-file=$CONFIG_FILE -e "$AFTER_SQL1"
    # mysql --defaults-file=$CONFIG_FILE -e "$AFTER_SQL2"
    # mysql --defaults-file=$CONFIG_FILE -e "$AFTER_SQL3"

    start=$(date -d "$start + 1 day" +%F)

done
